//
//  HomeCarouselFigure.m
//  NordsonTesco
//
//  Created by mac on 16/6/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "HomeCarouselFigure.h"
static int i = 0;
@implementation HomeCarouselFigure

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self homeCarouselFigure];
        [self addtimer];
    }
    return self;
}

//轮播图
- (void)homeCarouselFigure{
    i = 1;
    _scrollView = [[UIScrollView alloc]initWithFrame: CGRectMake(0,0,kScrernWidth,240)];
    _scrollView.contentSize = CGSizeMake(kScrernWidth*3,200);
    _scrollView.backgroundColor = [UIColor orangeColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    //    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollView];
    
    for (int i = 0; i < 3; i ++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.frame = CGRectMake(i*_scrollView.bounds.size.width,0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
        NSString *name = [NSString stringWithFormat:@"%d-V3", i + 1];
        imgView.image = [UIImage imageNamed:name];
        [_scrollView addSubview:imgView];
    }
    
    //    UIButton *carouselFigureButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, _scrollView.bounds.size.height)];
    //    [carouselFigureButton addTarget:self action:@selector(carouselFigureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    //    [self addSubview:carouselFigureButton];
    
    _pageControl = [[UIPageControl alloc]init];
    _pageControl.frame = CGRectMake(0,220, 120, 20);
    [_pageControl addTarget:self action:@selector(Changepag:) forControlEvents:UIControlEventEditingChanged];
    _pageControl.backgroundColor = [UIColor blackColor];
    _pageControl.numberOfPages = 3;
    _pageControl.currentPage = 0;
    [self addSubview:_pageControl];
}
//
- (void)Changepag:(UIPageControl *)pag
{
    [_scrollView setContentOffset:CGPointMake(pag.currentPage*self.frame.size.width,0 /*page.currentPage*480*/) animated:YES];
}
//添加定时器方法
-(void) addtimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    //返回当前的消息循环对象
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void) deleteTimer{
    [_timer invalidate];
    _timer = nil;
}

-(void)nextPage{
    [_scrollView setContentOffset:CGPointMake(i * kScrernWidth, 0) animated:YES];
    i ++;
    if (i == 3) {
        i = 0;
    }
}

#pragma  - mark UIScrollViewDelegate
//监听滚动的位置,改变pageCotrol的currentPage的值.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat scrollviewW =  scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollviewW / 2) /  scrollviewW;
    _pageControl.currentPage = page;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self deleteTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addtimer];
}
- (void)carouselFigureButtonAction{
    
    //    [self.view removeFromSuperview];
    
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
